package com.ebspp.server;

import java.util.List;
import java.util.Map;

import org.apache.thrift.TException;

import com.ebspp.server.resource.ClientResManager;
import com.ebspp.server.resource.ResourceManager;
import com.ebspp.thrift.DataMsg;
import com.ebspp.thrift.DataWrite;
import com.ebspp.thrift.Identity;
import com.ebspp.thrift.ResourceType;

/**
 * RPC class for handling remote client write calls
 * @author x23pan
 *
 */
public class WriteHandler implements DataWrite.Iface
{
	//write data
	@Override
	public List<DataMsg> writeEBSPP(List<DataMsg> data, ResourceType type, Identity id)
			throws TException {
		// TODO Auto-generated method stub
		ClientResManager resManager = ResourceManager.getResManager(id);
		if(resManager == null)
		{
			System.err.println("Resource unprovisioned");
			return data;
		}
		
		return resManager.write(data, type);
	}

	//resource provisioning
	@Override
	public boolean provision(Map<ResourceType, Integer> resource, Identity id)
			throws TException 
	{
		// TODO Auto-generated method stub
		if(resource == null || id == null)
			return false;
		System.out.println("Receive write provision from " + id.getKey());
		return ResourceManager.updateResource(resource, id);
	}
}
